<?php

class ItemsEditView extends AuthView
{
	private $types;
	
	public function __construct(ItemsEditModel $model)
	{
		parent::__construct($model);
		$this->addCss("adm");
		
		$this->types = array
		(
			ItemType::RESOURCE => "IT_RESOURCE",
			Itemtype::WEAPON => "IT_WEAPON",
		);
	}
	
	protected function title() { return TextManager::i()->text("PAGE_TITLE_EDIT_ITEMS", false); }
	protected function name() { return "items_edit"; }
	
	protected function content()
	{
		$itemList = $this->model()->itemList();
		
		$this->itemList($itemList);
	?>
	<br /><br />
	<?php
		$item = $this->model()->editItem();
	
		/*if (Input::request("action") == "edit")
		{
			$id = Input::request("id");
			if (array_key_exists($id, $itemList))
				$item = $itemList[$id];
		}*/
	
		$this->editForm($item);

		if ($this->model()->hasMsg()):
		
			$msg = $this->model()->msg();
			$text = sprintf(TextManager::tr($msg["text"], false), TextManager::tr($msg["item"]->name(), false));
	?>
	<br /><br />
	<div><?php echo $text; ?></div>
	<?php
		endif;
	}
	
	private function itemList($itemList)
	{
	?>
	<table border = "1">
		<tr>
			<th colspan = "2"><?php TextManager::tr("COL_ITEM_NAME"); ?></th>
			<th colspan = "2"><?php TextManager::tr("COL_ITEM_DESC"); ?></th>
			<th><?php TextManager::tr("COL_ITEM_MAKE_POINTS"); ?></th>
			<th><?php TextManager::tr("COL_ITEM_TYPE"); ?></th>
			<th><?php TextManager::tr("COL_ITEM_ACTIONS"); ?></th>
		</tr>
		<?php foreach ($itemList as $item): ?>
		<tr>
			<td><?php TextManager::tr($item->name()); ?></td>
			<td>
				<?php
				$langs = TextManager::i()->langs($item->name());
				foreach ($langs as $lang)
					echo $lang . ";";
				?>
			</td>
			<td><?php TextManager::tr($item->desc()); ?></td>
			<td>
				<?php
				$langs = TextManager::i()->langs($item->desc());
				foreach ($langs as $lang)
					echo $lang . ";";
				?>
			</td>
			<td><?php printf("%.2f", $item->makePoints()); ?></td>
			<td>
				<select>
				<?php foreach ($this->types as $type => $type_name): ?>
					<option value = "<?php echo $type; ?>"<?php if ($type == $item->type()): ?> selected<?php endif; ?>><?php TextManager::tr($type_name); ?></option>
				<?php endforeach; ?>
				</select>
			</td>
			<td>
				<a href = "<?php $this->url(array("action" => "remove", "id" => $item->id())); ?>"><?php TextManager::tr("REMOVE_ITEM"); ?></a>
				<br />
				<a href = "<?php $this->url(array("action" => "edit", "id" => $item->id())); ?>"><?php TextManager::tr("EDIT_ITEM"); ?></a>
			</td>
		</tr>
		<?php endforeach; ?>
	</table>
	<?php
	}
	
	private function editForm($editItem)
	{
		$errors = $this->model()->errors();
		
		$lang = TextManager::i()->lang();
		$name = sprintf("~~~[%s:][:%s]", $lang, $lang);
		$desc = sprintf("~~~[%s:][:%s]", $lang, $lang);
		$makePoints = 0.1;
		
		$urlParams = array("action" => "add");
		
		$submitText = "ADD_ITEM";
		
		if (!is_null($editItem))
		{
			$name = $editItem->name();
			$desc = $editItem->desc();
			$makePoints = $editItem->makePoints();
			$urlParams = array("action" => "edit", "id" => $editItem->id());
			
			$submitText = "ACCEPT";
		}
	?>
	<form action = "<?php $this->url($urlParams); ?>" method = "POST">
		<table border = "1">
			<tr>
				<th><?php TextManager::tr("COL_ITEM_NAME"); ?></th>
				<th><?php TextManager::tr("COL_ITEM_DESC"); ?></th>
				<th><?php TextManager::tr("COL_ITEM_MAKE_POINTS"); ?></th>
				<th><?php TextManager::tr("COL_ITEM_TYPE"); ?></th>
			</tr>
			<tr>
				<td>
					<input type = "text" name = "name" value = "<?php echo $name; ?>" />
				</td>
				<td>
					<textarea rows="4" cols="25" name = "desc"><?php echo $desc; ?></textarea>
				</td>
				<td<?php if (array_key_exists("make_points", $errors)): ?> class = "error"<?php endif;?>>
					<input type = "text" name = "make_points" value = "<?php printf("%.2f", $makePoints); ?>" />
				</td>
				<td<?php if (array_key_exists("type", $errors)): ?> class = "error"<?php endif;?>>
					<select name = "type">
					<?php foreach ($this->types as $type => $type_name): ?>
						<option value = "<?php echo $type; ?>"><?php TextManager::tr($type_name); ?></option>
					<?php endforeach; ?>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan = "4"><input type = "submit" value = "<?php TextManager::tr($submitText); ?>" /></td>
			</tr>
		<?php foreach ($errors as $error): ?>
			<tr><td class = "error" colspan = "4">*<?php TextManager::i()->text($error); ?></td></tr>
		<?php endforeach; ?>
		</table>
	</form>
	<?php
	}
}

?>